﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Linq.SqlClient;

namespace Hired
{
    /*  A CompanyAccount class used by companies to log in. */
    public class DBCompanyAccount
    {
        
        //Add a CompanyAccount
        public static bool AddCompanyAccount(CompanyAccount ca, Company c)
        {
            try
            {
                ca.CAccountID = "tempvalue";
                using (hireddbEntities db = new hireddbEntities())
                {
                    db.AddToCompanyAccounts(ca);
                    ca.Company = c;
                    db.SaveChanges();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        //Retrieve a CompanyAccount
        public static CompanyAccount RetrieveCompanyAccount(string accountID, bool company) 
        {
            using (hireddbEntities db = new hireddbEntities())
            {
                if (company)
                {
                    return
                        (from a in db.CompanyAccounts.Include("Company")
                         where a.CAccountID == accountID
                         select a).SingleOrDefault();
                }
                else
                {
                    return
                        (from a in db.CompanyAccounts
                         where a.CAccountID == accountID
                         select a).SingleOrDefault();
                }
            }
        }

        public static CompanyAccount RetrieveCompanyAccountByEmployerID(string employerPassportID, bool company)
        {
            using (hireddbEntities db = new hireddbEntities())
            {
                if (company)
                {
                    return
                        (from a in db.CompanyAccounts.Include("Company")
                         .Where(c => c.EmployerPassports.Any(ep => ep.EPassportID == employerPassportID))
                         select a).SingleOrDefault();
                }
                else
                {
                    return
                        (from a in db.CompanyAccounts
                         .Where(c => c.EmployerPassports.Any(ep => ep.EPassportID == employerPassportID))
                         select a).SingleOrDefault();
                }
            }
        }

        public static CompanyAccount RetrieveCompanyAccountByEmail(string email)
        {
            using (hireddbEntities db = new hireddbEntities())
            {
                return
                    (from ca in db.CompanyAccounts
                     where ca.Email == email
                     select ca).SingleOrDefault();
            }
        }

        //Update a CompanyAccount
        public static bool UpdateCompanyAccount(CompanyAccount ca) {
            try
            {
                using (hireddbEntities db = new hireddbEntities())
                {
                    CompanyAccount accountToUpdate = RetrieveCompanyAccount(ca.CAccountID, false);
                    db.Attach(accountToUpdate);
                    accountToUpdate.Email = ca.Email;
                    accountToUpdate.Activated = ca.Activated;
                    accountToUpdate.AllowMessages = ca.AllowMessages;
                    accountToUpdate.AllowNotifications = ca.AllowNotifications;
                    accountToUpdate.PrivacySettings = ca.PrivacySettings;
                    accountToUpdate.Password = ca.Password;

                    db.SaveChanges();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        //Delete a CompanyAccount
        public static bool DeleteCompanyAccount(CompanyAccount ca) {
            try
            {
                CompanyAccount accountToDelete = RetrieveCompanyAccount(ca.CAccountID, false);

                using (hireddbEntities db = new hireddbEntities())
                {
                    db.Attach(accountToDelete);
                    db.DeleteObject(accountToDelete);
                    db.SaveChanges();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        public static bool DeleteCompanyAccountById(string id)
        {
            try
            {
                CompanyAccount accountToDelete = RetrieveCompanyAccount(id, false);

                using (hireddbEntities db = new hireddbEntities())
                {
                    db.Attach(accountToDelete);
                    db.DeleteObject(accountToDelete);
                    db.SaveChanges();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }


        public static CompanyAccount RetrieveCompanyAccountByUsername(string username, bool company)
        {
            using (hireddbEntities db = new hireddbEntities())
            {
                if (company)
                {
                    return
                    (from ca in db.CompanyAccounts.Include("Company")
                     where ca.Username == username
                     select ca).SingleOrDefault();
                }
                else
                {
                    return
                    (from ca in db.CompanyAccounts
                     where ca.Username == username
                     select ca).SingleOrDefault();
                }
            }
        }
    }
             
}